﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchool.Models.Interfaces;

namespace GameSchool.Models.Repositories
{
    public class LevelRepository 
    {
        GameSchoolDBDataContext db = new GameSchoolDBDataContext();

        //Fetches all Levels
        public IQueryable<Level> GetLevels()
        {
            return db.Levels;
        }

        //Fetches all levels in a single course
        public IQueryable<Level> GetLevelsByCourseID(int? cID)
        {
            var lvl = (from Level in db.Levels
                       where Level.CourseID == cID
                       orderby Level.LevelNr descending
                       select Level);

            return lvl;
        }

        //Fetches all assignments in a single Level
        public IQueryable<Assignment> GetAssignmentsByLevel(int? levelID)
        {
            var result = (from Assignment in db.Assignments
                          where Assignment.LevelID == levelID
                          orderby Assignment.ID ascending 
                          select Assignment);
            return result;

        }

        //Fetches all Lectures in a certain Level
        public IQueryable<Assignment> GetLecturesByLevel(int? levelID)
        {
            var result = (from Assignment in db.Assignments
                          where Assignment.LevelID == levelID
                          where Assignment.AssignmentType == "Lecture"
                          orderby Assignment.ID ascending
                          select Assignment);
            return result;

        }

        //Fetches all Quizes in a certain Level
        public IQueryable<Assignment> GetQuizsByLevel(int? levelID)
        {
            var result = (from Assignment in db.Assignments
                          where Assignment.LevelID == levelID
                          where Assignment.AssignmentType == "Quiz"
                          orderby Assignment.ID ascending
                          select Assignment);
            return result;

        }

        //Fetches all HandIn's in a certain Level
        public IQueryable<Assignment> GetHandInsByLevel(int? levelID)
        {
            var result = (from Assignment in db.Assignments
                          where Assignment.LevelID == levelID
                          where Assignment.AssignmentType == "HandIn"
                          orderby Assignment.ID ascending
                          select Assignment);
            return result;

        }

        public void AddLevel(Level l)
        {
            db.Levels.InsertOnSubmit(l);
        }

        //Fetches a single Level by it's ID
        public Level GetLevelByID(int? lID)
        {
            var lvl = (from Level in db.Levels
                       where Level.ID == lID
                       select Level).SingleOrDefault();

            return lvl;
        }

        //Deletes a Level by it's ID
        public void DeleteLevel(int? lID)
        {
            var lvl = (from n in db.Levels
                       where n.ID == lID
                       select n).SingleOrDefault();
            if (lvl != null)
            {
                db.Levels.DeleteOnSubmit(lvl);
            }
        }

        //Helper function that fetches the number of assignments in a Level
        public int AssignmentsPerLevel(int? LevelID)
        {
            int result = (from s in db.Assignments
                          where s.LevelID == LevelID
                          select s).Count();
            return result;
        }


        public void Save()
        {
            db.SubmitChanges();
        }
    }
}